package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsRole;
import com.itqf.dto.CommonsDto;
import com.itqf.dto.PermissionDto;
import com.itqf.service.DtsRoleService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.Permission;
import com.itqf.utils.PermissionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/22 10:20
 * description:
 */

@RestController
@RequestMapping("admin/role/")
@CrossOrigin("*")
public class AdminDtsRoleController {


    @Autowired
    private DtsRoleService dtsRoleService;


    @GetMapping("options")
    public Object options(){

        List<Map<String, Object>> data = dtsRoleService.findRoleAll();

        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="查询")
    @GetMapping("list")
    public Object list(CommonsDto dto){

        Map<String, Object> data = dtsRoleService.findRoleByPage(dto);

        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="添加")
    @PostMapping("create")
    public Object create(@RequestBody DtsRole dtsRole){

        Object data = dtsRoleService.insertRole(dtsRole);

        return data;
    }


    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="修改")
    @PostMapping("update")
    public Object update(@RequestBody DtsRole dtsRole){

        Object data = dtsRoleService.updateRoleById(dtsRole);

        return data;
    }


    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsRole dtsRole){

        Object data = dtsRoleService.deleteRoleById(dtsRole);

        return data;
    }



    @Autowired
    private ApplicationContext applicationContext;


    @RequiresPermissions("admin:role:permission:list")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="权限列表")
    @GetMapping("permissions")
    public Object permissions(int roleId){

        //查询permission的原材料
        List<Permission> permissionList = PermissionUtil.listPermission(applicationContext,"com.itqf.controller");

        Object data = dtsRoleService.findPermissionList(permissionList,roleId);

        return ResponseUtil.ok(data);
    }



    @RequiresPermissions("admin:role:permission:update")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button="权限修改")
    @PostMapping("permissions")
    public Object permissions(@RequestBody PermissionDto permissionDto){

        //查询permission的原材料

        Object data = dtsRoleService.updateRolePermission(permissionDto);

        return data;
    }



}
